package com.cbs.android.function.input;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.cbs.android.component.application.activity.CBSDialogFragment;


/**
 * Created by zhuyj on 2015/1/20.
 * Modified by brochexu on 3/24/2015
 */

/**
 * 确认弹出框
 */
public class ConfirmDialogFragment extends CBSDialogFragment {
	private String title;
	private OnConfirmListener onConfirmListener;
	private String content;
	private Button cancelView;
	private Button submitView;
	private String submitMsg;
	private boolean cancel = false;
	private int layout = R.layout.cbs_input_dialog_confirm;

	public ConfirmDialogFragment setTitle(String title) {
		this.title = title;
		return this;
	}

	public ConfirmDialogFragment setOnConfirmListener(OnConfirmListener onConfirmListener) {
		this.onConfirmListener = onConfirmListener;
		return this;
	}

	public ConfirmDialogFragment setContent(String content) {
		this.content = content;
		return this;
	}

	public ConfirmDialogFragment setCancel(boolean cancel) {
		this.cancel = cancel;
		return this;
	}

	public ConfirmDialogFragment setsubmitMsg(String submitMsg) {
		this.submitMsg = submitMsg;
		return this;
	}

	public ConfirmDialogFragment setLayout(int layout) {
		this.layout = layout;
		return this;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(layout, container, false);

		// TODO need to check all code below
		// TODO layout
		TextView titleView = (TextView) view.findViewById(R.id.dialog_confirm_title);

		titleView.setText(title);
		TextView contentView = (TextView) view.findViewById(R.id.dialog_confirm_content);
		if (content != null && !content.equals("null")) {
			contentView.setText(content);
			contentView.setVisibility(View.VISIBLE);
		}
		cancelView = (Button) view.findViewById(R.id.dialog_confirm_cancel);
		submitView = (Button) view.findViewById(R.id.dialog_confirm_submit);
		cancelView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onCancelListener.onClick(ConfirmDialogFragment.this);
			}
		});
		submitView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				submitView.setEnabled(false);
				cancelView.setEnabled(false);
				onConfirmListener.onConfirm(ConfirmDialogFragment.this);
			}
		});
		if (!TextUtils.isEmpty(submitMsg)) {
			submitView.setText(submitMsg);
		}
		if (cancel) {
			cancelView.setVisibility(View.VISIBLE);
		}
		return view;
	}

	@Override
	public int show(FragmentTransaction transaction, String tag) {
		submitView.setEnabled(true);
		cancelView.setEnabled(true);
		return super.show(transaction, tag);
	}

	public interface OnConfirmListener {
		void onConfirm(CBSDialogFragment dialogFragment);
	}
}
